package com.loubii.sunshineSchool.ui.avtivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.loubii.sunshineSchool.FinalVirable;
import com.loubii.sunshineSchool.R;
import com.loubii.sunshineSchool.httpRequest.HttpBin;
import com.loubii.sunshineSchool.ui.dialog.ListPopWindow;
import com.loubii.sunshineSchool.util.DensityUtil;
import com.loubii.sunshineSchool.util.EditTextClearTools;

import java.util.List;

import retrofit2.Call;
import retrofit2.Response;


public class SSRegActivity extends AppCompatActivity {
    private EditText userName, passWord, rePassword, realNameEt, sid;
    private TextView userSchool;
    private ImageView unameClear, pwdClear, repwdClear;
    private TextView userLogin;
    private Button register;
    private RelativeLayout chooseSchool;
    private ListPopWindow mListPopWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        init();
        ViewClick();
    }

    private void ViewClick() {

        chooseSchool.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListPopWindow = new ListPopWindow(SSRegActivity.this, 4);
                mListPopWindow.setAnchorView(userSchool);
                mListPopWindow.setHorizontalOffset(DensityUtil.dip2px(5));
                mListPopWindow.setAdapter(new BaseAdapter() {
                    private List<String> mPopList = FinalVirable.schools;


                    @Override
                    public int getCount() {
                        return mPopList.size();
                    }

                    @Override
                    public Object getItem(int i) {
                        return null;
                    }

                    @Override
                    public long getItemId(int i) {
                        return 0;
                    }

                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {
                        ViewHolder holder;

                        if (convertView == null) {
                            convertView = LayoutInflater.from(SSRegActivity.this).inflate(R.layout.item_chart_popup, null);
                            holder = new ViewHolder();
                            holder.mTvClassify = (TextView) convertView.findViewById(R.id.tv_classify);
                            convertView.setTag(holder);
                        } else {
                            holder = (ViewHolder) convertView.getTag();
                        }
                        holder.mTvClassify.setText(mPopList.get(position));
                        return convertView;
                    }

                    class ViewHolder {
                        TextView mTvClassify;
                    }
                });
                mListPopWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        //Logger.e("click " + position);
                        userSchool.setText(FinalVirable.schools.get(position));
                        mListPopWindow.dismiss();
                    }
                });
                mListPopWindow.show();
            }
        });

        userLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                userLogin.setTextColor(Color.rgb(0, 0, 0));
                Intent intent = new Intent(getApplicationContext(), SSLoginActivity.class);
                startActivity(intent);
                finish();
                overridePendingTransition(R.anim.push_right_out, R.anim.push_right_in);
            }
        });

        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String username = userName.getText().toString();
                final String password = passWord.getText().toString();
                final String repassword = rePassword.getText().toString();
                final String school = userSchool.getText().toString();
                final String sid = passWord.getText().toString();
                final String realName = realNameEt.getText().toString();
                if (username.isEmpty()) {
                    Toast.makeText(getApplicationContext(), "帐号不能为空", Toast.LENGTH_LONG).show();
                    return;
                } else if (password.isEmpty()) {
                    Toast.makeText(getApplicationContext(), "密码不能为空", Toast.LENGTH_LONG).show();
                    return;
                } else if (school.isEmpty()) {
                    Toast.makeText(getApplicationContext(), "请选择学校", Toast.LENGTH_LONG).show();
                    return;
                } else if (sid.isEmpty()) {
                    Toast.makeText(getApplicationContext(), "学号不能为空", Toast.LENGTH_LONG).show();
                    return;
                } else if (realName.isEmpty()) {
                    Toast.makeText(getApplicationContext(), "请输入真实姓名", Toast.LENGTH_LONG).show();
                    return;
                } else if (!password.equals(repassword)) {
                    Toast.makeText(getApplicationContext(), "两次密码输入不一致", Toast.LENGTH_LONG).show();
                    return;
                }
                HttpBin.register(getApplicationContext(), username, password, realName, sid, school, new HttpBin.ResponseListener<Boolean>() {

                    @Override
                    public void onResponse(Call<Boolean> call, Response<Boolean> response) {
                        if(response.body()){
                            Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_LONG).show();
                            Intent intent = new Intent(getApplicationContext(), SSLoginActivity.class);
                            startActivity(intent);
                        }else {
                            Toast.makeText(getApplicationContext(), "用户名已存在，请更改~", Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<Boolean> call, Throwable t) {

                    }
                });
            }
        });
    }

    private void init() {
        chooseSchool = (RelativeLayout) findViewById(R.id.rl_userSchool);
        userSchool = (TextView) findViewById(R.id.et_userSchool);
        userName = (EditText) findViewById(R.id.et_userName);
        passWord = (EditText) findViewById(R.id.et_password);
        rePassword = (EditText) findViewById(R.id.et_repassword);
        unameClear = (ImageView) findViewById(R.id.iv_unameClear);
        pwdClear = (ImageView) findViewById(R.id.iv_pwdClear);
        repwdClear = (ImageView) findViewById(R.id.iv_repwdClear);
        userLogin = (TextView) findViewById(R.id.link_signup);
        register = (Button) findViewById(R.id.btn_login);
        sid = (EditText) findViewById(R.id.et_stuId);
        realNameEt = (EditText) findViewById(R.id.et_userRealName);
        EditTextClearTools.addClearListener(userName, unameClear);
        EditTextClearTools.addClearListener(passWord, pwdClear);
        EditTextClearTools.addClearListener(rePassword, repwdClear);
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent(getApplicationContext(), SSLoginActivity.class);
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.push_right_out, R.anim.push_right_in);
    }
}
